package com.hui.servlet.classes;

import com.hui.entity.Class;
import com.hui.service.ClassService;
import com.hui.util.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/07/09/15:04
 * @Description: 获取班级信息
 */
@WebServlet(name = "classesServlet", urlPatterns = "/classes")
public class ClassesServlet extends HttpServlet {
    private ClassService classService = ServiceFactory.getClassService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        List<Class> classList = null;
        // id为空，则获取所有
        if (id == null) {
            classList = classService.getAllClass();
        } else {
            classList = classService.getClassById(Integer.parseInt(id));
        }
        // 将数据放入session中
        req.getSession().setAttribute("classList", classList);
        // 重定向到classes.jsp
        resp.sendRedirect("classes.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
